package calorie.com.calorie.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.lxh.baselibray.dialog.AlertDialog;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.RegexUtils;
import com.lxh.baselibray.util.SizeUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.lxh.baselibray.view.PswText;

import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;
import calorie.com.calorie.R;
import calorie.com.calorie.contact.AddUserContact;
import calorie.com.calorie.response.ResultDTO;
import calorie.com.calorie.util.StaticValue;
import retrofit2.Response;

public class CreateAccountActivity extends MvpActivity<AddUserContact.AddUserPresent> implements AddUserContact.IAddUserView {
    @BindView(R.id.btn_confirm)
    public Button btn_confirm;
    @BindView(R.id.et_account)
    public EditText et_account;
    @BindView(R.id.et_pwd)
    public EditText et_pwd;

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_create_account;
    }

    @Override
    public void getResultDate(Response<ResultDTO> resultDTOResponse) {
        String code = resultDTOResponse.body().getCode();
        String msg = resultDTOResponse.body().getMsg();
        if (code.equals("200")) {

//            ToastUtils.showLongToast(context, "添加成功");
            successTipDialog();//成功弹窗
            this.finish();
            return;
        }
        ToastUtils.showLongToast(context, msg);
        failedTipDialog();//失败弹窗

    }

    //创建成功弹窗
    AlertDialog successDialog = null;

    public void successTipDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_success)
                .setWidthAndHeight(SizeUtils.dp2px(context, 100), SizeUtils.dp2px(context, 107))
                .setText(R.id.tx_tip, "创建成功");
        successDialog = builder.create();
        successDialog.show();
    }

    //创建失败弹窗
    AlertDialog failedDialog = null;

    public void failedTipDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_failed)
                .setWidthAndHeight(SizeUtils.dp2px(context, 100), SizeUtils.dp2px(context, 107))
                .setText(R.id.tx_tip, "创建失败！");
        failedDialog = builder.create();
        failedDialog.show();
    }



    @Override
    protected AddUserContact.AddUserPresent createPresent() {
        return new AddUserContact.AddUserPresent();
    }

    @OnClick(R.id.btn_confirm)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_confirm:
                String account = et_account.getText().toString().trim();
                String password = et_pwd.getText().toString().trim();
                if (ObjectUtils.isEmpty(account)) {
                    ToastUtils.showLongToast(context, "请输入帐户");
                    return;
                } else if (ObjectUtils.isEmpty(password)) {
                    ToastUtils.showLongToast(context, "请输入密码");
                    return;
                } else if (!Pattern.matches("^[a-zA-Z0-9]{4,20}$", account)) {
                    ToastUtils.showLongToast(context, "账号格式为4到20位数字或者字母");
                    return;
                } else if (!RegexUtils.isLoginPwd(password)) {
                    ToastUtils.showLongToast(context, getString(R.string.login_pwd_regex));
                    return;
                }
                showDealDialog(account,password);
                break;
        }
    }

    AlertDialog dealDialog = null;

    private void showDealDialog(String account,String password) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_deal_pwd)
                .setWidthAndHeight(SizeUtils.dp2px(context, 279), SizeUtils.dp2px(context, 157))
                .setOnClickListener(R.id.tv_sure, v -> {
                    PswText psw = dealDialog.getView(R.id.pt_deal_pwd);
                    String dealPwd = psw.getPsw();
                    mPresent.addUser(StaticValue.user.getId(), account, password,dealPwd);
                    dealDialog.dismiss();
                }).setOnClickListener(R.id.tv_cancle, v -> dealDialog.dismiss());
        dealDialog = builder.create();
        dealDialog.show();
    }
}
